package com.XinYun.Library.modules.Login.Servlet;

import com.XinYun.Library.modules.Login.Entity.User;
import com.XinYun.Library.modules.Login.Service.Impl.UserServiceImpl;
import com.XinYun.Library.modules.Login.Service.SendEmail;
import com.XinYun.Library.modules.Login.Service.UserService;
import com.XinYun.Library.modules.commons.ResponseWrapper.ResponseWrapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Random;

import static org.apache.poi.ss.formula.ptg.NamePtg.sid;

@WebServlet(name = "FindPwdServlet1", value = "/FindPwdServlet1")
public class FindPwdServlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BufferedReader reader = request.getReader();
        String readerLine = reader.readLine();
        JSONObject jsonObject = JSON.parseObject(readerLine);
        String sid = jsonObject.getString("Register_account");
        String mail = jsonObject.getString("Register_mail");

        HttpSession session = request.getSession();
        session.setAttribute("sid",sid);
        //3.通过邮箱找到用户信息
        UserService service = new UserServiceImpl();
        String email = null;
        try {
            email = service.findUserBySid(sid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //5.判断学号与邮箱是否匹配
        if (email != null && email.equals(mail)){
            //服务器通知浏览器不要缓存
            response.setHeader("pragma","no-cache");
            response.setHeader("cache-control","no-cache");
            response.setHeader("expires","0");

            String base = "0123456789ABCDEFGHIJKLMNOPQRSDUVWXYZabcdefghijklmnopqrsduvwxyz";
            int size = base.length();
            Random r = new Random();
            StringBuilder code = new StringBuilder();
            for(int i=1;i<=6;i++){
                //产生0到size-1的随机值
                int index = r.nextInt(size);
                //在base字符串中获取下标为index的字符
                char c = base.charAt(index);
                //将c放入到StringBuffer中去
                code.append(c);
            }
            //把验证码保存在session中，方便后续判断
            session.setAttribute("EmailCode",code.toString());
            //实例化一个发送邮件的对象
            SendEmail mySendMail = new SendEmail();
            //发送邮件
            mySendMail.sendMail(mail, "您正在找回密码，验证码为：" + code.toString() +"\n请正确输入！如非本人操作，请忽略。");
            ResponseWrapper responseWrapper = null;
            responseWrapper = ResponseWrapper.SendSuccess();
            String s = JSON.toJSONString(responseWrapper);
            PrintWriter writer = response.getWriter();
            writer.println(s);
            System.out.println(s);
            // request.getRequestDispatcher("repassword2.jsp").forward(request,response);
        }else {
            ResponseWrapper responseWrapper = null;
            responseWrapper = ResponseWrapper.SuitFail();
            String s = JSON.toJSONString(responseWrapper);
            PrintWriter writer = response.getWriter();
            writer.println(s);
            System.out.println(s);
            //request.getRequestDispatcher("repassword1.jsp").forward(request,response);
        }

    }
}
